package com.zx._12_算法.剑指offer._2022;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/
 */
public class 栈的压入弹出序列 {

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();

        int pop = 0;
        for (int i = 0; i < pushed.length; i++) {
            stack.push(pushed[i]);

            // 判断是否是出栈的位置 i
            while (!stack.isEmpty() && stack.peek() == popped[pop]) {
                stack.pop();
                pop++;
            }
        }

        return stack.isEmpty();

    }
}
